package com.music.action.vedio;

import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.music.action.BaseAction;
import com.music.action.BaseKey;
import com.music.common.ParamUtil;
import com.music.domain.PageInfo;
import com.music.domain.Video;
import com.music.vedio.service.VedioService;

/**
 * 视频相关action
 * @author LFSenior
 *
 */
public class VedioAction extends BaseAction{
	
	@Autowired
	VedioService vedioService;
	/**
	 * 跳转到视频页面
	 * @return
	 */
	@Action(value="vedio",results={@Result(name="vedio",location="vedio.jsp")})
	public String redicterVedioAction(){
		return "vedio";
	}
	
	@Action(value="findvedio",results={@Result(name="findvedio",location="video/item.jsp")})
	public String findVedioAction(){
		/* 获取相关数据 */
		PageInfo pageInfo = (PageInfo) session.get(BaseKey.VIDEO_PAGE);
		if(pageInfo==null){
			pageInfo=new PageInfo();
			pageInfo.setCurr_total("1");
		}
		request.put(BaseKey.VIDEO_LIST, findVideo(null, pageInfo));
		session.put(BaseKey.VIDEO_PAGE, pageInfo);
		return "findvedio";
	}
	
	
	/**
	 * 查询所有数据
	 * @param param
	 * @param page
	 * @return
	 */
	public List<Video> findVideo(Map param,PageInfo page){
		return vedioService.findVedioAll(param, page);
	}
	
	
	@Action(value = "perpageVideo", results = { @Result(name = "perpage", location = "video/item.jsp") })
	public String perPage() throws Exception{
		PageInfo pageInfo = (PageInfo) session.get(BaseKey.VIDEO_PAGE);
		if(pageInfo==null){
			pageInfo=new PageInfo();
			pageInfo.setCurr_total("1");
		}
		pageInfo.setCurr_total(String.valueOf(Integer.parseInt(pageInfo.getCurr_total())-1));
		if(Integer.parseInt(pageInfo.getCurr_total())<=0){
			pageInfo.setCurr_total("1");
		}
		request.put(BaseKey.VIDEO_LIST, findVideo(null,pageInfo));
		session.put(BaseKey.VIDEO_PAGE, pageInfo);
		return "perpage";
	}
	
	@Action(value = "nextpageVideo", results = { @Result(name = "nextpage", location = "video/item.jsp") })
	public String nextPage() throws Exception{
		PageInfo pageInfo = (PageInfo) session.get(BaseKey.VIDEO_PAGE);
		if(pageInfo==null){
			pageInfo=new PageInfo();
			pageInfo.setCurr_total("1");
		}
		pageInfo.setCurr_total(String.valueOf(Integer.parseInt(pageInfo.getCurr_total())+1));
		if(Integer.parseInt(pageInfo.getCurr_total())>=Integer.parseInt(pageInfo.getTotal())){
			pageInfo.setCurr_total(pageInfo.getTotal());
		}
		request.put(BaseKey.VIDEO_LIST, findVideo(null, pageInfo));
		session.put(BaseKey.VIDEO_PAGE, pageInfo);
		return "nextpage";
	}
}
